$("document").ready(function(){
	//请补充下面函数url参数
/*	ajaxRequest("#user_name","#name_result","","R007","商铺名字");//名字重复的status值定为R007，可以改动
	ajaxRequest("#user_phone","#phone_result","","R001","商家电话");
	ajaxRequest("#user_email","#email_result","","R002","商家邮箱");*/

	$("#register").addClass("active");
	$("#user_phone").blur(function(){
		if ($("#user_phone").val() === "") {
			$("#user_phone").addClass("form-error");
			$("#phone_result").html("电话号码不能为空");
		}else if (!checkPhone($("#user_phone").val())){
			$("#user_phone").addClass("form-error");
			$("#phone_result").html("电话号码格式不正确");
		}else {
			$.ajax({
				type:'post',
				url:'/login/checkUser.action',
				dataType:'json',
				data:{
					user_phone:$("#user_phone").val(),
					user_type :'1'
				},
				success:function(data){
					if (data.status === "User_Not_Registered") {
						$("#user_phone").removeClass("form-error");
						$("#phone_result").html("");
					} else if (data.status === "User_Empty") {
						$("#user_phone").addClass("form-error");
						$("#phone_result").html(data.msg);
					}else if (data.status === "User_Exist"){
						$("#user_phone").addClass("form-error");
						$("#phone_result").html(data.msg);
					}else if (data.status === "Check_Fail"){
						sweetAlert("ERROR","服务异常","error");
					}else if (data.status === "Incorrect_Phone"){
						sweetAlert("WARNING","电话号码格式不正确","warning");
					}else {
						sweetAlert("发生错误","","error");
					}
				},
				error:function(jqXHR){
					//alert("发生错误" + jqXHR.status);
					sweetAlert("发生错误",jqXHR.status,"error");
				}
			});
		}
		submitCondition();
	});


	//点击营业执照上传触发的事件
	$("#upload").click(function(){
		$("#user_img").trigger("click");
	});


	//当input[type=file]的域发生改变，进行上传,该input的name="img_upload"
	$("#user_img").change(function(){
		var file = this.files[0];
		//判断类型是不是图片
		if(!/image\/\w+/.test(file.type)){
			sweetAlert("WARNING","请确保文件为图像类型","warning");
			return;
		}
		var objUrl = getObjectURL(file) ;
		if (objUrl) {
			$("#img_url").val(objUrl);
			$("#img_show").removeClass("hide");
			$("#img_show").attr('src',objUrl);
		}
		submitCondition();

	});

	//商家邮箱为空
	$("#user_email").blur(function(){
		if ($("#user_email").val() === "") {
			$("#user_email").addClass("form-error");
			$("#email_result").html("邮箱不能为空");
		}else if (!checkEmail($('#user_email').val())){
			$("#user_email").addClass("form-error");
			$("#email_result").html("邮箱格式不正确");
		}else {
			$("#user_email").removeClass("form-error");
			$("#user_email").addClass("form-success");
			$("#email_result").html("");
		}
		submitCondition();
	});

	//商家名称为空
	$("#user_name").blur(function(){
		if ($("#user_name").val() === "") {
			$("#user_name").addClass("form-error");
			$("#name_result").html("商家名称不能为空");
		} else {
			$("#user_name").removeClass("form-error");
			$("#user_name").addClass("form-success");
			$("#name_result").html("");
		}
		submitCondition();
	});


	//密码为空
	$("#user_pwd").blur(function(){
		if ($("#user_pwd").val() === "") {
			$("#user_pwd").addClass("form-error");
			$("#pwd_result").html("密码不能为空");
		}else if (!checkPassword($("#user_pwd").val())){
			$("#user_pwd").addClass("form-error");
			$("#pwd_result").html("密码需含有字母、数字以及密码长度在6-15位之间");
		} else {
			$("#user_pwd").removeClass("form-error");
			$("#user_pwd").addClass("form-success");
			$("#pwd_result").html("");
		}
		submitCondition();
	});
	//确认密码为空或确认密码与密码不相同
	$("#pwd_confirm").blur(function(){
		if ($("#pwd_confirm").val() === "") {
			$("#pwd_confirm").addClass("form-error");
			$("#confirm_result").html("确认密码不能为空");
		//下面确认密码条件有误
		} else if ($("#user_pwd").val() !== $("#pwd_confirm").val()) {
			$("#pwd_confirm").addClass("form-error");
			$("#confirm_result").html("密码与确认密码不相同");
		} else {
			$("#pwd_confirm").removeClass("form-error");
			$("#pwd_confirm").addClass("form-success");
			$("#confirm_result").html("");
		};
		submitCondition();
	});
});
//函数参数为（input的id，结果的id，发送到服务器的url，返回的status值，为空选项的名字）五个
function ajaxRequest(id,resultId,actionUrl,status,words) {
	$(id).blur(function(){
		if ($(id).val() === "") {
			$(id).addClass("form-error");
			$(resultId).html(words+"不能为空");
		} else {
			//返回json的错误语句为{“status”:”R007”,”msg”:”该商铺名字已被使用”,”results”:null}
			$.ajax({
				type:"GET",
				//下面地址由后台补上
				url:actionUrl+$(id).val(),
				dataType:"json",
				success:function(data){
					if (data.status === status) {
						$(id).addClass("form-error");
						$(resultId).html(data.msg);
					} else {
						$(id).removeClass("form-error");
						$(id).addClass("form-success");
						$(result).html("");
					}
				},
				error:function(jqXHR){
					alert("发生错误" + jqXHR.status);
				},
			});
		};
		submitCondition();
	});
}
//表单存在没填选项或填写有错误，提交按钮为disabled
function submitCondition(){
	if ( $("*").hasClass("form-error")) {
		$("#submit").addClass("disabled");
	} else {
		$("#submit").removeClass("disabled");
	}
}

/*function readFile(obj){
	var file = obj.files[0];
	//判断类型是不是图片
	if(!/image\/\w+/.test(file.type)){
		sweetAlert("WARNING","请确保文件为图像类型","warning");
		return false;
	}
	var reader = new FileReader();
	reader.readAsDataURL(file);
	reader.onload = function(e){
		$("#img_show").removeClass("hide");
		$("#img_show").attr('src',objUrl);
	}
}*/
/**
 * http://blog.csdn.net/mhmyqn/article/details/25561535
 * http://blog.csdn.net/stven_king/article/details/22957001
 * http://blog.csdn.net/inuyasha1121/article/details/51915742
 * http://blog.csdn.net/magi1201/article/details/41362857
 * https://my.oschina.net/lenglingx/blog/607972
 * http://www.cnblogs.com/taoys/archive/2010/12/30/1922186.html
 * http://blog.csdn.net/fantastic_/article/details/54325986
 * @returns {boolean}
 */
function registerUser() {
	var user_name = $('#user_name').val();
	var user_phone = $('#user_phone').val();
	var user_email = $('#user_email').val();
	var user_pwd = $('#user_pwd').val();
	var pwd_confirm = $('#pwd_confirm').val();
	var user_type = 1;
	if(user_name == "" || user_phone == "" || user_email == "" || user_pwd == ""){
		sweetAlert("WARNING","请填写用户完整信息","warning");
		return false;
	}
	if (user_pwd != pwd_confirm){
		sweetAlert("WARNING","前后密码不一致","warning");
		return false;
	}
	var formData = new FormData();
	formData.append("user_name",user_name);
	formData.append("user_phone",user_phone);
	formData.append("user_email",user_email);
	formData.append("user_password",user_pwd);
	formData.append("password_confirm",pwd_confirm);
	formData.append("user_type",user_type);
	formData.append("user_img",$('#user_img')[0].files[0]);
	$.ajax({
		url:'/upload',
		type:'post',
		cache:false,
		data:formData,
		processData:false,
		contentType:false,
		timeout:60000,
		beforeSend:function () {
			$("#submit").empty();
			$("#submit").html("<img height='30' width='30' src='/image/loading.gif'/>");
			$("#submit").addClass("disabled");
		},
		success:function (data) {
			if (data.status === "User_Empty"){
				sweetAlert("WARNING","请填写用户完整信息","warning");
			}else if (data.status === "Different_PWD"){
				$("#pwd_confirm").addClass("form-error");
				$("#confirm_result").html("前后密码不一致");
			}else if (data.status === "Image_Needed"){
				sweetAlert("WARNING","请上传商家执照","warning");
			}else if (data.status === "Image_Fail"){
				sweetAlert("ERROR","图片上传失败,请重新上传","error");
			}else if (data.status === "Register_Fail"){
				swal({
					title: "ERROR",
					text: "注册失败",
					type: "error",
					showCancelButton: false,
					closeOnConfirm: true,
					confirmButtonText: "确定"
				}, function() {
					window.location.reload();
				});
				setTimeout(function(){
					window.location.reload();
				},3000);
			}else if (data.status === "Register_Repeated"){
				sweetAlert("WARNING","该号码已经注册","warning");
			}else if (data.status === "Check_Fail"){
				sweetAlert("ERROR","请填写用户完整信息","error");
			}else if (data.status === "Register_Fail"){
				sweetAlert("ERROR","注册失败","error");
			}else if (data.status === "Upload_Fail"){
				sweetAlert("ERROR","图片上传失败,请重新上传","error");
			}else if (data.status === "Register_Success"){
				swal({
					title: "Success",
					text: "注册成功",
					type: "success",
					showCancelButton: false,
					closeOnConfirm: true,
					confirmButtonText: "确定"
				}, function() {
					window.location.href = data.url;
				});
				setTimeout(function(){
				 window.location.href = data.url;
				 },3000);
			}else if (data.status === "Incorrect_Phone_Format"){
				$("#user_phone").addClass("form-error");
				$("#phone_result").html("电话号码格式不正确");
			}else if (data.status === "Incorrect_Email_Format"){
				$("#user_email").addClass("form-error");
				$("#email_result").html("邮箱格式不正确");
			}else if (data.status === "Incorrect_PWD_Format"){
				$("#user_pwd").addClass("form-error");
				$("#pwd_result").html("密码需含有字母、数字以及密码长度在6-15位之间");
			}else {
				swal({
					title: "ERROR",
					text: "注册异常",
					type: "error",
					showCancelButton: false,
					closeOnConfirm: true,
					confirmButtonText: "确定"
				}, function() {
					window.location.reload();
				});
				setTimeout(function(){
					window.location.reload();
				},3000);
			}
		},
		complete:function () {
			$("#submit").empty();
			$("#submit").html("注册");
			$("#submit").removeClass("disabled");
		},
		error:function (jqXHR) {
			$("#submit").empty();
			$("#submit").html("注册");
			$("#submit").removeClass("disabled");
			swal({
				title: jqXHR.status,
				text: "发生错误",
				type: "error",
				showCancelButton: false,
				closeOnConfirm: true,
				confirmButtonText: "确定"
			}, function() {
				window.location.reload();
			});
			setTimeout(function(){
				window.location.reload();
			},3000);
		}
	});
	submitCondition();
	return false;
}

function getObjectURL(file) {
	var url = null;
	if (window.createObjectURL != undefined) { // basic
		url = window.createObjectURL(file);
	} else if (window.URL != undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file);
	} else if (window.webkitURL != undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file);
	}
	return url;
}

function checkPhone(phone) {
	var rex = /^1[3|4|5|8][0-9]\d{4,8}$/
	if (rex.test(phone)){
		return true;
	}else {
		return false;
	}
}
function checkEmail(email) {
	var rex = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
	if (rex.test(email))
		return true;
	else
		return false;
}
function checkPassword(password) {
	var  rex = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/
	if (rex.test(password)){
		return true;
	}else {
		return false;
	}
}
